package com.study.jdbc.junit;

import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.sql.DataSource;

import org.junit.Test;

import com.study.jdbc.datasource.MyDataSource;
import com.study.jdbc.datasource.MyDataSource2;
import com.study.jdbc.datasource.MyDataSource3;
import com.study.jdbc.wrapper.ConnectionInvocationHandler;

/**
* @Name: MyDataSourceTest
* @Description: 自定义数据源测试类
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-2-18 上午10:45:34 
* @Version: V1.0
 */
public class MyDataSourceTest {
	
	private DataSource dataSource01 = new MyDataSource() ;
	private DataSource dataSource02 = new MyDataSource2() ;
	private DataSource dataSource03 = new MyDataSource3() ;
	
	/**
	* @Name: wrapperConnection
	* @Description: 包装Connection的close()方法
	* 用途：
	* 	扩展Connection的close()方法的功能，调用MyConnection类的close()方法：将数据库连接放回到连接池中
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-18 下午2:18:42
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void wrapperConnection() {
		Connection con = null ;
		Statement s = null ;
		ResultSet rs = null ;
		try {
			//从数据源获取数据库连接对象
			con = dataSource01.getConnection() ;
			System.out.println(con.getClass().getName()); //MyConnection实现类：com.study.jdbc.datasource.MyConnection
			String sql = "select id, name, price, detail, pic, createtime from tb_items ; " ;
			s = con.createStatement() ;
			rs = s.executeQuery(sql) ;
			while(rs.next()) {
				System.out.println("=========================================");
				System.out.println(rs.getInt("id"));
				System.out.println(rs.getString("name"));
				System.out.println(rs.getFloat("price"));
				System.out.println(rs.getString("detail"));
				System.out.println(rs.getString("pic"));
				System.out.println(rs.getDate("createtime"));
			}
		} catch (Exception e) {
			e.printStackTrace() ;
		} finally {
			//释放资源：将数据库连接重新放入到连接池中
			if(con != null) {
				try {
					con.close() ;
				} catch (Exception e) {
					e.printStackTrace() ;
				} finally {
					con = null ;
				}
			}
			//释放资源
			if(s != null) {
				try {
					s.close() ;
				} catch (Exception e) {
					e.printStackTrace() ;
				} finally {
					s = null ;
				}
			}
			//释放资源
			if(rs != null) {
				try {
					rs.close() ;
				} catch (Exception e) {
					e.printStackTrace() ;
				} finally {
					rs = null ;
				}
			}
		}
	}
	
	/**
	* @Name: proxyConnection
	* @Description: 动态代理Connection实现类的close()方法
	* 用途：
	* 	将数据库连接放回到连接池中
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-18 下午2:19:59
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void proxyConnection() {
		Connection con = null ;
		Statement s = null ;
		ResultSet rs = null ;
		try {
			//从数据源获取数据库连接对象
			con = dataSource02.getConnection() ; 
			System.out.println(con.getClass().getName()); //Connection接口实现类：com.mysql.jdbc.JDBC4Connection
			String sql = "select id, name, price, detail, pic, createtime from tb_items ; " ;
			s = con.createStatement() ;
			rs = s.executeQuery(sql) ;
			while(rs.next()) {
				System.out.println("=========================================");
				System.out.println(rs.getInt("id"));
				System.out.println(rs.getString("name"));
				System.out.println(rs.getFloat("price"));
				System.out.println(rs.getString("detail"));
				System.out.println(rs.getString("pic"));
				System.out.println(rs.getDate("createtime"));
			}
		} catch (Exception e) {
			e.printStackTrace() ;
		} finally {
			//释放资源：将数据库连接重新放入到连接池中
			//创建Connection接口实现类的代理对象
			ConnectionInvocationHandler handler = new ConnectionInvocationHandler(con, dataSource02) ;
			Connection proxy = (Connection) Proxy.newProxyInstance(con.getClass().getClassLoader(), 
					new Class[] {Connection.class}, 
					handler) ;
			System.out.println(proxy.getClass().getName()); //Connection接口实现类的动态代理对象：com.sun.proxy.$Proxy2
			if(con != null) {
				try {
					proxy.close() ; //使用jdk动态代理，执行Connection的close()方法将数据库连接重新放入到连接池中
				} catch (Exception e) {
					e.printStackTrace() ;
				} finally {
					con = null ;
				}
			}
			//释放资源
			if(s != null) {
				try {
					s.close() ;
				} catch (Exception e) {
					e.printStackTrace() ;
				} finally {
					s = null ;
				}
			}
			//释放资源
			if(rs != null) {
				try {
					rs.close() ;
				} catch (Exception e) {
					e.printStackTrace() ;
				} finally {
					rs = null ;
				}
			}
		}
	}
	
	/**
	* @Name: proxyConnection02
	* @Description: 动态代理Connection实现类的close()方法：使用匿名内部类的形式进行封装
	* 用途：
	* 	将数据库连接放回到连接池中
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-2-18 下午2:19:59
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void proxyConnection02() {
		Connection con = null ;
		Statement s = null ;
		ResultSet rs = null ;
		try {
			//从数据源获取数据库连接对象
			con = dataSource03.getConnection() ; 
			System.out.println(con.getClass().getName()); //Connection接口实现类的动态代理对象：com.sun.proxy.$Proxy2
			String sql = "select id, name, price, detail, pic, createtime from tb_items ; " ;
			s = con.createStatement() ;
			rs = s.executeQuery(sql) ;
			while(rs.next()) {
				System.out.println("=========================================");
				System.out.println(rs.getInt("id"));
				System.out.println(rs.getString("name"));
				System.out.println(rs.getFloat("price"));
				System.out.println(rs.getString("detail"));
				System.out.println(rs.getString("pic"));
				System.out.println(rs.getDate("createtime"));
			}
		} catch (Exception e) {
			e.printStackTrace() ;
		} finally {
			//释放资源：将数据库连接重新放入到连接池中
			if(con != null) {
				try {
					con.close() ; //使用jdk动态代理，执行Connection的close()方法将数据库连接重新放入到连接池中
				} catch (Exception e) {
					e.printStackTrace() ;
				} finally {
					con = null ;
				}
			}
			//释放资源
			if(s != null) {
				try {
					s.close() ;
				} catch (Exception e) {
					e.printStackTrace() ;
				} finally {
					s = null ;
				}
			}
			//释放资源
			if(rs != null) {
				try {
					rs.close() ;
				} catch (Exception e) {
					e.printStackTrace() ;
				} finally {
					rs = null ;
				}
			}
		}
	}
	
	
	


}



















